@extends('admin.main')

@section('content')
<div class="wrapper wrapper-content">
    <div class="m-b">
        <a href="javascript:;" class="btn btn-default" onclick="history.back();"><i class="fa fa-angle-double-left"></i> 返回列表</a>
    </div>
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>填空题 <i class="fa fa-angle-double-right m-l m-r"></i> 编辑</h5>
        </div>
        <div class="ibox-content">
            <form id="formEdit" method="post" class="form-horizontal" onsubmit="return false;">
                <div class="form-group">
                    <label class="col-lg-2 control-label">题目：</label>
                    <div class="col-lg-10">
                        <div id="ueditor_topic" class="summernote">{!!$topic!!}</div>
                    </div>
                </div>
                <div class="form-group m-b-n">
                    <label class="col-lg-2 control-label">答案：</label>
                    <div id="fillblankAnwers" class="col-lg-10">
                        @foreach($answers as $i => $val)
                            @if($i < 1)
                            <div class="input-group">
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-white">{{$i+1}}</button>
                                </span>
                                <input type="text" name="answer[]" value="{{$val}}" placeholder="答案" class="form-control" />
                            </div>
                            @else
                            <div class="input-group">
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-white">{{$i+1}}</button>
                                </span>
                                <input type="text" name="answer[]" value="{{$val}}" placeholder="答案" class="form-control" />
                                <span class="input-group-btn">
                                    <button type="button" class="btn btn-danger js-removeanswer">
                                        <i class="fa fa-times"></i>
                                    </button>
                                </span>
                            </div>
                            @endif
                        @endforeach
                    </div>
                </div>
                <div class="form-group m-t-n">
                    <div class="col-lg-8 col-lg-offset-2">
                        <a href="javascript:;" onclick="addAnswer()">
                            <i class="fa fa-plus"></i>
                            添加答案
                        </a>
                    </div>
                </div>
                <div class="hr-line-dashed"></div>
                <div class="form-group">
                    <div class="col-lg-4 col-lg-offset-2">
                        <input type="hidden" name="id" value="{{$id}}" />
                        <button class="btn btn-warning btn-w-m" type="submit">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 填空题题目答案 -->
<script id="tplAnswerRow" type="text/html">
    <div class="input-group">
        <span class="input-group-btn">
            <button type="button" class="btn btn-white"></button>
        </span>
        <input type="text" name="answer[]" placeholder="答案" class="form-control" />
        <span class="input-group-btn">
            <button type="button" class="btn btn-danger js-removeanswer">
                <i class="fa fa-times"></i>
            </button>
        </span>
    </div>
</script>
@endsection

@section('pageheader')
<link href="/assets/iCheck/custom.css" rel="stylesheet">
<link href="/assets/summernote/summernote.css" rel="stylesheet">
<link href="/assets/summernote/summernote-bs3.css" rel="stylesheet">
@endsection

@section('pagescript')
<script src="/assets/iCheck/icheck.min.js"></script>
<script src="/assets/summernote/summernote.min.js"></script>
<script>
    $("#side-menu li[rel='examtopic']").addClass("active")
        .parents("ul").addClass("in").parents("li").addClass("active");

    // Summernote 编辑器上传图片
    var sendFile = function (file, editor, welEditable) {
        var data = new FormData();
        data.append("upfile", file, file.name || ('blob.' + file.type.substr('image/'.length)));
        $.ajax({
            data: data,
            type: "POST",
            dataType: "json",
            url: "{{url('ueditor?action=uploadimage')}}",
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
                editor.insertImage(welEditable, data.url);
            }
        });
    };

    // 加载 Summernote 编辑器
    var loadSummernote = function(editor){
        editor.summernote({
            onImageUpload: function(files, editor, welEditable) {
                sendFile(files[0], editor, welEditable);
            }
        });
    };

    // 增加填空题答案
    var addAnswer = function(){
        $("#fillblankAnwers").append(template("tplAnswerRow", null));
        $("#fillblankAnwers").find(".btn-white").each(function(i){
            $(this).text(i+1);
        });
    };

    // 删除填空题答案
    $("#fillblankAnwers").delegate(".js-removeanswer", "click", function(){
        $(this).parents(".input-group").eq(0).remove();
        $("#fillblankAnwers").find(".btn-white").each(function(i){
            $(this).text(i+1);
        });
    });

    $(function(){
        loadSummernote($('#ueditor_topic'));
    });

    $("#formEdit").on("submit", function(){
        var form = this, formdata = new FormData();
        formdata.append("topic", $("#ueditor_topic").code());
        $.each($(form).serializeArray(), function(i, item) {
            formdata.append(item.name, item.value);
        });
        $.ajax({
            url: "{{url('admin/exam/update')}}",
            type: "post",
            processData: false,
            contentType: false,
            dataType: "json",
            data: formdata,
            success:function(){
                window.location.href = document.referrer;
            },
            error:failure
        });
        return false;
    });
</script>
@endsection
